/**
 * The contents of this file are subject to the OpenMRS Public License
 * Version 1.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://license.openmrs.org
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * Copyright (C) OpenMRS, LLC.  All Rights Reserved.
 */
package org.openmrs.module.clinicalsummary.web;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openmrs.module.clinicalsummary.SummaryConstants;
import org.openmrs.util.OpenmrsUtil;
import org.springframework.web.bind.ServletRequestUtils;

/**
 *
 */
public class GenerateSummariesSecretServlet extends HttpServlet {
	
	private static final Log log = LogFactory.getLog(GenerateSummariesSecretServlet.class);
	
	private static final int BUFFER_SIZE = 16;
	
	private static final long serialVersionUID = 1L;
	
	/**
	 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		if (log.isDebugEnabled())
			log.debug("Retrieving secret file ...");

		String secretFileName = ServletRequestUtils.getStringParameter(request, "secretFile");
		
		File parentSecretFile = OpenmrsUtil.getDirectoryInApplicationDataDirectory(SummaryConstants.ENCRYPTION_LOCATION);
		File secretFile = new File(parentSecretFile, secretFileName);
		
		//Prepare response
		response.setHeader("Content-Disposition", "attachment; filename=" + secretFileName);
		response.setContentType("text/plain");
		response.setContentLength((int) secretFile.length());
		response.flushBuffer();
		
		BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(secretFile));
		
		ReadableByteChannel input = Channels.newChannel(inputStream);
		WritableByteChannel output = Channels.newChannel(response.getOutputStream());
		ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE);
		
		while (input.read(buffer) != -1) {
			buffer.flip();
			output.write(buffer);
			buffer.clear();
		}
		
		input.close();
		output.close();
		
		secretFile.delete();
	}
	
	/**
	 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	
}
